package gu.simplemq.stomp;

import gu.simplemq.Constant;
import gu.simplemq.pool.BaseMQInstance;

/**
 * STOMP对象工厂类用于获取producer/consumer,publisher/subscriber,table对象<br>
 * 应用程序结束时会自动清除所有subscriber和consumer
 * @author guyadong
 *
 */
public class StompFactory implements Constant {

	private StompFactory() {}
	private static  final BaseMQInstance<StompConsumer,StompPoolLazy> CONSUMERS = 
			new BaseMQInstance<StompConsumer,StompPoolLazy>(){
		@Override
		protected void beforeDelete(StompConsumer r) {
			try {
				r.close();
			} catch (Throwable e) {
				e.printStackTrace();
			}
	}};
	private static final BaseMQInstance<StompSubscriber,StompPoolLazy> SUBSCRIBERS = 
			new BaseMQInstance<StompSubscriber,StompPoolLazy>(){
		@Override
		protected void beforeDelete(StompSubscriber r) {
			try {
				r.close();
			} catch (Throwable e) {
				e.printStackTrace();
			}
	}};
	private static final BaseMQInstance<StompProducer,StompPoolLazy> PRODUCERS = 
			new BaseMQInstance<StompProducer,StompPoolLazy>(){
		@Override
		protected void beforeDelete(StompProducer r) {
			r.close();
	}};
	private static final BaseMQInstance<StompPublisher,StompPoolLazy> PUBLISHERS = 
			new BaseMQInstance<StompPublisher,StompPoolLazy>(){

		@Override
		protected void beforeDelete(StompPublisher r) {
			r.close();
		}};
			
	/**
	 * 返回 {@link StompPoolLazy}对应的{@link StompConsumer}实例
	 * @param poolLazy
	 * @return
	 * @see gu.simplemq.pool.BaseMQInstance#getInstance(gu.simplemq.stomp.StompPoolLazy)
	 */
	public static StompConsumer getConsumer(StompPoolLazy poolLazy) {
		return CONSUMERS.getInstance(poolLazy);
	}
	
	/** 
	 * 返回{@link StompPoolLazy}默认实例对应的{@link StompConsumer}实例
	 * @see  {@link StompPoolLazy#getDefaultInstance()}
	 */
	public static StompConsumer getConsumer() {
		return CONSUMERS.getInstance(StompPoolLazys.getDefaultInstance());
	}
	/**
	 * 返回 {@link StompPoolLazy}对应的{@link StompSubscriber}实例
	 * @param poolLazy
	 * @return {@link StompSubscriber}实例
	 */
	public static StompSubscriber getSubscriber(StompPoolLazy poolLazy) {
		return SUBSCRIBERS.getInstance(poolLazy);
	}
	/**
	 * 返回 {@link StompPoolLazy}对应的{@link StompProducer}实例
	 * @param jedisPoolLazy
	 * @return
	 * @see gu.simplemq.pool.BaseMQInstance#getInstance(gu.simplemq.activemq.StompPoolLazy)
	 */
	public static StompProducer getProducer(StompPoolLazy jedisPoolLazy) {
		return PRODUCERS.getInstance(jedisPoolLazy);
	}
	/** 
	 * 返回{@link StompPoolLazy}默认实例对应的{@link StompProducer}实例
	 * @see  {@link StompPoolLazy#getDefaultInstance()}
	 */
	public static StompProducer getProducer() {
		return PRODUCERS.getInstance(StompPoolLazys.getDefaultInstance());
	}
	/** 
	 * 返回{@link StompPoolLazy}默认实例对应的{@link StompSubscriber}实例
	 * @see  {@link StompPoolLazy#getDefaultInstance()}
	 * @return {@link StompSubscriber}实例
	 */
	public static StompSubscriber getSubscriber() {
		return SUBSCRIBERS.getInstance(StompPoolLazys.getDefaultInstance());
	}
	/**
	 * 返回 {@link StompPoolLazy}对应的{@link StompPublisher}实例
	 * @param poolLazy
	 * @return {@link StompPublisher}实例
	 */
	public static StompPublisher getPublisher(StompPoolLazy poolLazy) {
		return PUBLISHERS.getInstance(poolLazy);
	}
	/** 
	 * 返回{@link StompPoolLazy}默认实例对应的{@link StompPublisher}实例
	 * @see  {@link StompPoolLazy#getDefaultInstance()}
	 * @return {@link StompPublisher}实例
	 */
	public static StompPublisher getPublisher() {
		return PUBLISHERS.getInstance(StompPoolLazys.getDefaultInstance());
	}
	/**
	 * 关闭并删除所有consumer,subscriber,producer
	 */
	public synchronized static void closeAll(){
		CONSUMERS.clearInstances();
		SUBSCRIBERS.clearInstances();
		PUBLISHERS.clearInstances();
		PRODUCERS.clearInstances();
	}
	static {
		// JVM 结束时自动清除所有consumer和subscriber,producer对象
		Runtime.getRuntime().addShutdownHook(new Thread(){

			@Override
			public void run() {
				try {
					closeAll();
					// 销毁连接池对象
					StompPoolLazys.closeAll();
				} catch (Throwable e) {
					logger.error(e.toString());
				}
			}			
		});
	}
}
